<template>
  <div class="login_container">
    <el-row>
      <!-- xs < 768px 占多少份栅格 -->
      <el-col :span="12" :offset="0" :xs="0"></el-col>
      <el-col :span="12" :offset="0" :xs="24">
        <el-form
          class="login-form"
          :model="form"
          ref="ruleFormRef"
          :rules="rules"
        >
          <h1>硅谷甄选</h1>
          <h2>用于后台管理二次开发模块</h2>
          <el-form-item prop="username">
            <slot name="babel"><span class="title">用户名</span></slot>
            <el-input
              v-model="form.username"
              :prefix-icon="User"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <slot name="babel"><span class="title">密码</span></slot>
            <el-input
              v-model="form.password"
              show-password
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>
          <el-form-item class="button-box">
            <el-button
              style="width: 100%"
              :loading="btn_loding"
              type="primary"
              @click="submitForm(ruleFormRef)"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { FormInstance, FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTimePeriod } from '@/utils/time'

let userStore = useUserStore()
let router = useRouter()
const route = useRoute()
const btn_loding = ref(false)

const ruleFormRef = ref<FormInstance>()
const form = reactive({
  username: 'admin',
  password: 'atguigu123',
})

const rules = reactive<FormRules>({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      btn_loding.value = true
      try {
        // 保证登录成功
        await userStore.userLogin(form)

        btn_loding.value = false

        // 跳转首页
        router.push({ path: (route.query['redirect'] as string) || '/' })
        // 成功提示
        ElNotification({
          type: 'success',
          message: '欢迎回来',
          title: `Hi，${getTimePeriod()}`,
        })
      } catch (error) {
        btn_loding.value = false

        // 登录失败提示信息
        ElNotification({
          type: 'error',
          message: (error as Error).message,
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form {
    position: absolute;
    padding: 40px 20px;
    top: 30vh;
    margin-left: 10vw;
    width: 30%;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    h1 {
      font-size: 22px;
      color: white;
      opacity: 0.6;
      margin-bottom: 20px;
    }
    h2 {
      margin: 10px 0px;
      font-size: 18px;
      color: white;
      opacity: 0.6;
    }
  }
  .title {
    font-size: 14px;
    color: white;
    font-weight: bold;
    opacity: 0.9;
  }
  .button-box {
    margin-top: 30px;
  }
}
</style>
